package com.music.mysongs.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.music.mysongs.data.ChatMsg

class MsgAdapter(private val mChatMsgList: List<ChatMsg>) :
    RecyclerView.Adapter<MsgAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View =
            LayoutInflater.from(parent.context)
                .inflate(com.music.mysongs.R.layout.item_recycler_chat, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val msg = mChatMsgList[position]
        if (msg.type == ChatMsg.TYPE_RECEIVED) {
            holder.leftLayout.visibility = View.VISIBLE
            holder.rightLayout.visibility = View.GONE
            holder.leftMsg.text = msg.content
        } else if (msg.type == ChatMsg.TYPE_SENT) {
            holder.leftLayout.visibility = View.GONE
            holder.rightLayout.visibility = View.VISIBLE
            holder.rightMsg.text = msg.content
        }
    }

    override fun getItemCount(): Int {
        return mChatMsgList.size
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var leftLayout: LinearLayout = view.findViewById(com.music.mysongs.R.id.layoutRoomLeft_item)
        var rightLayout: LinearLayout =
            view.findViewById(com.music.mysongs.R.id.layoutRoomRight_item)
        var leftMsg: TextView = view.findViewById(com.music.mysongs.R.id.txtTalkerMsg_item)
        var rightMsg: TextView = view.findViewById(com.music.mysongs.R.id.txtTalkMsg_item)
        var leftName: TextView = view.findViewById(com.music.mysongs.R.id.txtRoomTalker_item)
        var rightName: TextView = view.findViewById(com.music.mysongs.R.id.txtRoomTalk_item)
    }
}